﻿Imports Microsoft.Office.Interop

Public Class ExcelBinding
    Dim xlApp As Excel.Application

    Public Function OpenObject() As Boolean
        Const XL_NOTRUNNING As Long = 429

        On Error GoTo ShowName_Err
        xlApp = GetObject(, "Excel.Application")
        xlApp.Visible = True

ShowName_End:
        Return True
ShowName_Err:
        If Err() = XL_NOTRUNNING Then
            xlApp = New Excel.Application '
            xlApp.Workbooks.Add()
            Resume Next
        Else
            MsgBox(Err.Number & " - " & Err.Description)
            Return False
        End If
        Resume ShowName_End
    End Function

    Public Sub CloseObject()
        xlApp.Quit()
        xlApp = Nothing
    End Sub

    Public Sub OpenFile(ByVal filename As String)
        xlApp.Visible = True
        xlApp.Workbooks.Open(filename)
    End Sub

    Public Sub Closefile(ByVal filename As String)
        xlApp.Workbooks(filename).Close(False)
    End Sub
End Class
